<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\User;
use App\Comms;
use Auth;
class PersonalController extends Controller
{
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[]];

	//	我的收藏
    public function sc(){
       
        //拿到uid
        $uid=Auth::user()->uid;
    	$data=[];
    	$ad=[];
    	//查出用户的信息
    	$user=DB::table('users')->where('uid',$uid)->first();
    	//根据uid查出收藏表中用户收藏的文章
    	$art=DB::table('scs')->where('uid',$uid)->get();
    	// 得到aid放入一个数组里，为了查询对应的文章信息
    	foreach($art as $vel){
    		$data[]=$vel->aid;
    		}
    	//print_r($data);
    	//查询收藏的文章信息
    	$arts=DB::table('arts')->whereIn('aid',$data)->get();
    	//根据aid在dsps表中查出文章评论和收藏数量，并和文章信息放入一个数组，形成一个三维数组
    	foreach($arts as $v){
    		
    		$dsp=DB::table('dsps')->where('aid',$v->aid)->get();
    		$dsp[]=$v;    		
    		$ad[]=$dsp;
    		// //我的收藏数量，写入txt，在模板中读出
    		// $xi=count($ad);
    		// file_put_contents('b.txt', $xi);
    		// //echo $xi;
    		
     	}
       	//print_r($ad);
    	//print_r($arts);
    	return view('user/sc',['ad'=>$ad],['user'=>$user]);
	}


    

    //我的评论
    public function pl(){

        //拿到uid
        $uid=Auth::user()->uid;
       	//两种查询方法
    	$user=User::where('uid',$uid)->first();
    	$user=DB::table('users')->where('uid',$uid)->first();
    	//print_r($user);
     
      	$data=[];
       	//查出'我'所有评论的内容
    	$pl=DB::table('comms')->where('uid',$uid)->get();

    	$art=DB::table('scs')->where('uid',$uid)->get();
    	//得到每篇评论对应的文章名
    	foreach($pl as $vel){
       		$arts = DB::table('arts')->where('aid',$vel->aid)->paginate(5);
       		if($vel->parentid){
       			$arts[]=$vel;
    			$data[]=$arts;
       		}
    		
    		}
    	//print_r($data);
    	//我的评论数量
    	// $pll=count($data);
    	// //echo $pll;
    	// file_put_contents('c.txt', $pll);
    	//print_r($pl);
    	return view('user/pl',['data'=>$data],['user'=>$user]);
    //原生联表查询法
    // $q=DB::select('select * from comms inner join arts on comms.aid=arts.aid where comms.uid='.$uid);
    // print_r($q);
    }



    
    //取消评论
    public function dpl($aid){
         //拿到uid
        //$uid=Auth::user()->uid;
        
        DB::table('comms')->where('aid',$aid)->update(['parentid'=>0]);
        return redirect('user/pl');
        //echo $aid;
    }




    //修改基本信息
     public function shezhi(){
     	//拿到uid
        $u=Auth::user()->uid;
        
        //print_r($u);
     	$user=DB::table('users')->where('uid',$u)->first();
     	//print_r($user);die;
      	//dd($user);
    	return view('user/shezhi',['user'=>$user]);
    }
    public function shezhipost(){
        //拿到uid
        $uid=Auth::user()->uid;
        echo $uid;
    	//将提交过来的post中token去掉
    	unset($_POST['_token']);
    	print_r($_POST);
   		$uid=$_POST['uid'];
   		//直接将post写入数据局库
		DB::table('users')->where('uid',$uid)->update($_POST);
		return redirect('user/shezhi');
    }


    //更改密码
    public function password(){
    	//拿到uid
        $uid=Auth::user()->uid;
    	//查询用户信息
    	$user=DB::table('users')->select('name','password','head_path','uid')->where('uid',$uid)->first();
    	return view('user/password',['user'=>$user]);
    }

    public function passwordpost(){
    	//print_r($_POST['pd']);
	   	//print_r($uid);
        //拿到uid
        $uid=Auth::user()->uid;
	   	//查询用户信息
    	$p=DB::table('users')->where('uid',$uid)->first();
    	//var_dump($p->password);
    	//判断用户密码是否匹配
    	if($_POST['pd']==$p->password){
    		//判断两次密码是否一致
    		if($_POST['new_pwd']===$_POST['repeat_pwd']){
    			DB::table('users')->where('uid',$uid)->update(['password'=>$_POST['new_pwd']]);
    			return '修改成功';
    		}else{
    			return '两次输入的密码不一致';
    		}
    	}else{
    		return '密码错误';
    	}
    }


    //更改头像
    public function head(){
        //拿到uid
        $uid=Auth::user()->uid;
    	$user=DB::table('users')->where('uid',$uid)->first();
    	return view('user/head',['user'=>$user]);
    	
    }
}
